﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Services;
using Pap.Services.FinancialServices;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class AssociationDuesViewModel : IViewModel
    {
        #region [ Members ]
        /// <summary>
        /// 
        /// </summary>
        ILoanRepository _LoanRepository;
        /// <summary>
        /// 
        /// </summary>
        IWorkflowService _WorkflowService;
        /// <summary>
        /// 
        /// </summary>
        ILoanExecutionService _LoanExecutionService;
        /// <summary>
        /// 
        /// </summary>
        IAdministrationRepository _AdministrationRepository;

        private readonly IFinanceService _financeService;

        #endregion

        #region [ Constructor ]

        /// <summary>
        /// Initializes a new instance of the <see cref="AssociationDuesViewModel"/> class.
        /// </summary>
        /// <param name="loanRepository">The loan repository.</param>
        /// <param name="workflowService">The workflow service.</param>
        /// <param name="loanExecutionService">The loan execution service.</param>
        /// <param name="AdminRepository">The admin repository.</param>
        /// <param name="financeService"></param>
        public AssociationDuesViewModel(ILoanRepository loanRepository, IWorkflowService workflowService, ILoanExecutionService loanExecutionService, IAdministrationRepository AdminRepository, IFinanceService financeService)
        {
            _LoanRepository = loanRepository;
            _WorkflowService = workflowService;
            _LoanExecutionService = loanExecutionService;
            _AdministrationRepository = AdminRepository;
            _financeService = financeService;
        }
        #endregion

        #region [ Methods ]

        public IEnumerable GetAllDues()
        {
            return SelectInstallemnt(_financeService.GetAllDuesForCommittee(RegistryInfo.CommitteeID));
        }
        public IEnumerable GetLateDues()
        {
            return SelectInstallemnt(_financeService.GetLateDuesForCommittee(RegistryInfo.CommitteeID));
        }
        public IEnumerable GetTodatDues()
        {
            return SelectInstallemnt(_financeService.GetTodayDuesForCommittee(RegistryInfo.CommitteeID));
        }
        public IEnumerable GetDuesInInterval(DateTime start,DateTime end)
        {
            return SelectInstallemnt(_financeService.GetDuesForCommitteeByDate(RegistryInfo.CommitteeID, start, end));
        }

        #endregion

        #region [ Helpers ]
        private IEnumerable SelectInstallemnt(IEnumerable<ILoanExecutionInstallment> lst)
        {
            return from installmet in lst
                   let totalPaid = installmet.LoanInstallmentPaymentLst.Sum(s=>s.PaymentAmount)
                   select new
                   {
                       installmet.LoanExecution.LoanID,
                       installmet.LoanExecution.CustomerLoan.CustomerObj.CustomerName,
                       installmet.LoanExecution.CustomerLoan.LoanNumber,
                       installmet.InstallmentDate,
                       installmet.OriginalInstallmentAmount,
                       installmet.InterestInstallmentAmount,
                       installmet.PenaltyAmount,
                       TodayDues = installmet.OriginalInstallmentAmount + installmet.InterestInstallmentAmount,
                       Total = installmet.OriginalInstallmentAmount + installmet.InterestInstallmentAmount + installmet.PenaltyAmount - totalPaid,
                       installmet.PenaltyDays,
                       installmet.LoanExecutionInstallmentID
                   };
        }
        #endregion
    }
}
